package p.correo;

import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;

public class EmisorCorreo {

    static String _mailHost = "smtp.arnet.com.ar";
    static String _fileName = "zip.zip";
    static String _filePath = "C:\\zip.zip";
    static String _de     = "pablo17x@arnet.com.ar";
    static String _para       = "jbenedet@dc.uba.ar";
    static String _asunto  = "Asunto";
    static String _cuerpo     = "Cuerpo";

    public static void main (String argv[]){
        sendMsg(_para, _asunto, _cuerpo, _de, _filePath, _fileName);
    }

    public static void sendMsg(String  pPara, String pAsunto, String pCuerpo,
                               String pDe, String pFilePath){
        sendMsg(pPara, pAsunto, pCuerpo, pDe, pFilePath, null);
    }

    public static void sendMsg(String  pPara, String pAsunto,
                                String pCuerpo, String pDe){
        sendMsg(pPara, pAsunto, pCuerpo, pDe, null);
    }

    public static void sendMsg(String  pPara, String pAsunto, String pCuerpo,
                              String pDe, String pFilePath, String pFileName){
        try {
            System.out.println("Enviando meil a :    " + pPara);
    	    System.out.println("From:      " + pDe);
    	    System.out.println("Subject:    " + pAsunto);
	        System.out.println("BodyText:  " + pCuerpo);

            // mail variables
            Properties props;
            Session    session;

            // create some properties and get the default Session
            props = System.getProperties();
            props.put("mail.smtp.host", _mailHost);

            session = Session.getDefaultInstance(props, null);
            session.setDebug(false);

            // create a mime message
            MimeMessage mail = new MimeMessage(session);
            mail.setFrom(new InternetAddress(pDe));
            InternetAddress[] address = {new InternetAddress(pPara)};
            mail.setRecipients(Message.RecipientType.TO, address);
            mail.setSubject(pAsunto);

            // create and fill the first message part (bodyText)
            MimeBodyPart bodyPart1 = new MimeBodyPart();
            bodyPart1.setText(pCuerpo);

            // create the Multipart and its parts to it
            Multipart multiPart = new MimeMultipart();
            multiPart.addBodyPart(bodyPart1);

            //Attach
            if (pFilePath != null){
                MimeBodyPart bodyPart2 = new MimeBodyPart();
                FileDataSource fds = new FileDataSource(pFilePath);

                bodyPart2.setDataHandler(new DataHandler(fds));
                if (pFileName != null)
                    bodyPart2.setFileName(pFileName);
                else
                    bodyPart2.setFileName(pFilePath);

                multiPart.addBodyPart(bodyPart2);
            }

            // add the Multipart to the message
            mail.setContent(multiPart);

            // set the Date: header
            mail.setSentDate(new Date());

            // send the message
            System.out.println("Casi Listo");
            Transport.send(mail);

            System.out.println("Listo");

      } catch (Exception mex){
    	  System.out.println(mex.toString());
	      mex.printStackTrace();
	    }
    }
}
